package com.xinchao.controller;

import com.xinchao.constant.DateConstant;
import com.xinchao.constant.ResponseConstant;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.sys.JsonResponse;
import com.xinchao.model.vo.IndexAdvertisementMonitorSumVO;
import com.xinchao.model.vo.IndexExposuresTouchWatchSumVO;
import com.xinchao.service.IndexService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.List;

/**
 * 首页Controller
 *
 * @author dxy
 * @date 2018/9/29 15:06
 */
@Controller
@RequestMapping(value = "/index")
public class IndexController {
	private static Logger logger = LoggerFactory.getLogger(IndexController.class);

	@Autowired
	private IndexService indexService;

	/**
	 * 获取首页曝光次数、触达人次、观看人次、平均观看时长、点位数
	 */
	@ResponseBody
	@RequestMapping(value = "/listAdvertisementMonitorSum")
	public Object listAdvertisementMonitorSum(Integer dateType) {
		if (dateType == null) {
			dateType = DateConstant.TODAY;
		}
		try {
			IndexAdvertisementMonitorSumVO indexAdvertisementMonitorSumVO = indexService.getIndexAdvertisementMonitorSumVO(dateType);
			return indexAdvertisementMonitorSumVO;
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("IndexController listAdvertisementMonitorSum 获取首页曝光次数、触达人次、观看人次、平均观看时长、点位数失败：" + e.getMessage());
			return e.getMessage();
		} catch (ParseException e) {
			e.printStackTrace();
			logger.error("IndexController listAdvertisementMonitorSum 获取首页曝光次数、触达人次、观看人次、平均观看时长、点位数失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, "系统错误，请联系管理员");
		}
	}

	/**
	 * 获取曝光次数、触达人次、观看人次列表(查询近30天的数据)
	 */
	@ResponseBody
	@RequestMapping(value = "/listExposuresTouchWatchSum")
	public Object listExposuresTouchWatchSum() {
		try {
			List<IndexExposuresTouchWatchSumVO> voList = indexService.listExposuresTouchWatchSum();
			return voList;
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error("IndexController listExposuresTouchWatchSum 获取曝光次数、触达人次、观看人次列表(查询近30天的数据)失败：" + e.getMessage());
			return e.getMessage();
		} catch (ParseException e) {
			e.printStackTrace();
			logger.error("IndexController listExposuresTouchWatchSum 获取曝光次数、触达人次、观看人次列表(查询近30天的数据)失败：" + e.getMessage());
			return new JsonResponse(ResponseConstant.CODE_FAIL, "系统错误，请联系管理员");
		}
	}

}
